package Experiment1;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

class JDialogDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("模式对话框测试");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        JButton button = new JButton("点击弹出模式对话框");
        button.addActionListener(e -> {
            Inner inner = new Inner();
            inner.showDialog(frame);
        });
        frame.getContentPane().add(button);
        frame.setVisible(true);
    }
    static class Inner {
        private JDialog dialog;
        public Inner() {
            dialog = new JDialog();
            dialog.setTitle("模式对话框");
            dialog.setSize(200, 150);
            dialog.setLocationRelativeTo(null);
            dialog.setModal(true);
            JButton closeButton = new JButton("关闭模式对话框");
            closeButton.addActionListener(e -> dialog.dispose());
            dialog.getContentPane().add(closeButton);
        }
        public void showDialog(JFrame parentFrame) {
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        }
    }
}